package com.proactive.webapp.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import com.proactive.webapp.dao.WorksheetCategoryDAO;
import com.proactive.webapp.entity.WorksheetCategory;

public class WorksheetCategoryDAOImpl implements WorksheetCategoryDAO{
	private DataSource dataSource;
	private Connection conn = null ;
	private ResultSet rs = null ;
	private PreparedStatement smt = null ;
	
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	@Override
	public List<WorksheetCategory> getList() {
		// TODO Auto-generated method stub
		List<WorksheetCategory> worksheetCategoryList = new ArrayList<WorksheetCategory>();
		String sql = "SELECT * FROM worksheet_category";
		 
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			rs = smt.executeQuery();
			while(rs.next()){
				WorksheetCategory worksheetCategory = new WorksheetCategory();
				worksheetCategory.setId(rs.getInt("worksheetCategoryID"));
				worksheetCategory.setName(rs.getString("worksheetCategoryName"));
				worksheetCategoryList.add(worksheetCategory);
			}
			rs.close();
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
		return worksheetCategoryList;
	}

}
